<?php
require_once ("BaseDAO.php");

// Song Value Object
class Song extends BaseVO {
	var $id;
	var $title;
	var $author;
	var $audio;
	var $info;
	var $key_played;
	var $active;

	// create a new VO
	function Song(
		$id= 0,
		$title= "",
		$author= "",
		$audio= "",
		$info= "",
		$key_played= 0,
		$active= ""
	) {
		$this->id = $id;
		$this->title = $title;
		$this->author = $author;
		$this->audio = $audio;
		$this->info = $info;
		$this->key_played = $key_played;
		$this->active = $active;
	}

	// compare to another VO
	function equals($vo) {
		return $this->id == $vo->id &&
			$this->title == $vo->title &&
			$this->author == $vo->author &&
			$this->audio == $vo->audio &&
			$this->info == $vo->info &&
			$this->key_played == $vo->key_played &&
			$this->active == $vo->active;
	}
	// copies another VO
	function copy($vo) {
		$this->id = $vo->id;
		$this->title = $vo->title;
		$this->author = $vo->author;
		$this->audio = $vo->audio;
		$this->info = $vo->info;
		$this->key_played = $vo->key_played;
		$this->active = $vo->active;
	}

	// output as a string
	function toString() {
		return $this->id . "," . $this->title . "," . $this->author . "," . $this->audio . "," . $this->info . "," . $this->key_played . "," . $this->active;
	}

	// output as XML node
	function toXML() {
		return "<row>\n" .
		"<id>$this->id</id>\n" .
		"<title>$this->title</title>\n" .
		"<author>$this->author</author>\n" .
		"<audio>$this->audio</audio>\n" .
		"<info>$this->info</info>\n" .
		"<key_played>$this->key_played</key_played>\n" .
		"<active>$this->active</active>\n" .
		"</row>\n";
	}

	// read from an html form
	function readForm($prefix="") {
		$this->id = $this->formHelper($prefix . "id", 0);
		$this->title = $this->formHelper($prefix . "title", "");
		$this->author = $this->formHelper($prefix . "author", "");
		$this->audio = $this->formHelper($prefix . "audio", "");
		$this->info = $this->formHelper($prefix . "info", "");
		$this->key_played = $this->formHelper($prefix . "key_played", 0);
		$this->active = $this->formHelper($prefix . "active", "");
	}

	// read from the query string
	function readQuery($prefix="") {
		$this->id = $this->queryHelper($prefix . "id", 0);
		$this->title = $this->queryHelper($prefix . "title", "");
		$this->author = $this->queryHelper($prefix . "author", "");
		$this->audio = $this->queryHelper($prefix . "audio", "");
		$this->info = $this->queryHelper($prefix . "info", "");
		$this->key_played = $this->queryHelper($prefix . "key_played", 0);
		$this->active = $this->queryHelper($prefix . "active", "");
	}

	// extra functions here
	function getPK() {
		return $this->id;
	}
}

// Song Data Access Object
class SongDAO extends BaseDAO {
	var $SQL_SELECT = "SELECT * FROM `song` ";
	var $SQL_COUNT = "SELECT count(*) AS cnt FROM `song` ";
	// insert with no value for an auto_increment field.
	var $SQL_INSERT = "INSERT INTO `song` (title,author,audio,info,key_played,active) VALUES ('%A','%B','%C','%D',%E,'%F')";
	var $SQL_UPDATE = "UPDATE `song` SET ";
	var $SQL_DELETE = "DELETE FROM `song` WHERE id=%A";

	// default constructor
	function SongDAO($dbserver="", $dbname="", $dbuser="", $dbpass="") {
		// calls the parent constructor which
		// makes the database connection.
		parent::BaseDAO($dbserver, $dbname, $dbuser, $dbpass);
	}

	// returns a single Song obeject
	function get($id) {    
	$this->sql = $this->SQL_SELECT . "WHERE (id='" . $this->safeEscapeStr($id) . "')";
		$this->exec($this->sql);
		if($this->numRows() > 0 ) {
			$row = $this->getObject();
			$vo = new Song(
				$row->id, 
				$row->title, 
				$row->author, 
				$row->audio, 
				$row->info, 
				$row->key_played, 
				$row->active
			);
			return $vo;
		} else {
			return null;    
		}
	}

	// returns an array of Song objects
	// given a complete SQL query 
	// The SQL Query needs to be safely escaped by the caller!
	function findBySQL($sql) {
		$voList = array();
		$this->sql = $sql;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Song(
				$row->id, 
				$row->title, 
				$row->author, 
				$row->audio, 
				$row->info, 
				$row->key_played, 
				$row->active
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Song objects
	// value for $where is some thing like "(xxx = 'abc') AND (zzz='123')"
	// value for $order_by is a comma separated list of columns "company, name"
	// the where and order clauses should be safely escaped by the caller!
	function findWhere($where = "", $orderby = "") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		if(strlen($where) > 0 ) {
			$where = "WHERE (" . $where . ") ";
		}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Song(
				$row->id, 
				$row->title, 
				$row->author, 
				$row->audio, 
				$row->info, 
				$row->key_played, 
				$row->active
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// returns an array of Song objects based on the example
	function findExample($example, $orderby="") {
		$this->sql = $this->SQL_SELECT;
		$voList = array();
		$where = "";

		$tmp = "";
		if($example->id != 0) $tmp .= "id = " . $this->safeEscapeStr($example->id) . " and ";
		if(strlen($example->title) > 0) $tmp .= "title like '%" . $this->safeEscapeStr($example->title) . "%' and ";
		if(strlen($example->author) > 0) $tmp .= "author like '%" . $this->safeEscapeStr($example->author) . "%' and ";
		if(strlen($example->audio) > 0) $tmp .= "audio like '%" . $this->safeEscapeStr($example->audio) . "%' and ";
		if(strlen($example->info) > 0) $tmp .= "info like '%" . $this->safeEscapeStr($example->info) . "%' and ";
		if($example->key_played != 0) $tmp .= "key_played = " . $this->safeEscapeStr($example->key_played) . " and ";
		if(strlen($example->active) > 0) $tmp .= "active like '%" . $this->safeEscapeStr($example->active) . "%' and ";
		if ($tmp != "") {$where = "WHERE (" . substr($tmp, 0, strlen($tmp)-4) . ") ";}
		if(strlen($orderby) > 0) {
			$orderby = "ORDER BY " . $orderby;
		} else {
			$orderby = "ORDER BY 1";
		}
		$this->sql .= $where . " " . $orderby;
		$this->exec($this->sql);
		while($row = $this->getObject()) {
			$vo = new Song(
				$row->id, 
				$row->title, 
				$row->author, 
				$row->audio, 
				$row->info, 
				$row->key_played, 
				$row->active
			);
			array_push($voList, $vo);
		}
		return $voList;
	}

	// insert a record from a vo
	function insert(&$vo) {
		$this->sql = $this->SQL_INSERT;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->title), $this->sql);
		$this->sql = str_replace("%B", $this->safeEscapeStr($vo->author), $this->sql);
		$this->sql = str_replace("%C", $this->safeEscapeStr($vo->audio), $this->sql);
		$this->sql = str_replace("%D", $this->safeEscapeStr($vo->info), $this->sql);
		$this->sql = str_replace("%E", $this->safeEscapeStr($vo->key_played), $this->sql);
		$this->sql = str_replace("%F", $this->safeEscapeStr($vo->active), $this->sql);

		$this->exec($this->sql);
		$vo->id = $this->lastOID();
		return $this->affectedRows();
	}

	// update a record from a vo
	function update($vo) {
		$this->sql = $this->SQL_UPDATE;
		$this->sql .= "title = '" . $this->safeEscapeStr($vo->title) . "', ";
		$this->sql .= "author = '" . $this->safeEscapeStr($vo->author) . "', ";
		$this->sql .= "audio = '" . $this->safeEscapeStr($vo->audio) . "', ";
		$this->sql .= "info = '" . $this->safeEscapeStr($vo->info) . "', ";
		$this->sql .= "key_played = " . $this->safeEscapeStr($vo->key_played) . ", ";
		$this->sql .= "active = '" . $this->safeEscapeStr($vo->active) . "' ";

		$this->sql .= "WHERE id=" . $this->safeEscapeStr($vo->id);

		$this->exec($this->sql);
		return $this->affectedRows();
	}

	//save an object to the databse (update or insert as needed)
	function save(&$vo) {
		if ($vo->id <= 0)
			return $this->insert($vo);
		else
			return $this->update($vo);
	}

	// delete a record from a vo
	function delete($vo) {
		$this->sql = $this->SQL_DELETE;
		$this->sql = str_replace("%A", $this->safeEscapeStr($vo->id), $this->sql);
		$this->exec($this->sql);
		return $this->affectedRows();
	}

}

